package com.hygj.controller;

import com.hygj.pojo.File;
import com.hygj.pojo.FilePicture;
import com.hygj.service.FilePictureService;
import com.hygj.service.FileService;
import com.hygj.util.*;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * @author hygl
 */
@Controller
@RequestMapping
public class FileController {

    @Autowired
    private FileService fileService;
    @Autowired
    private FilePictureService filePictureService;



    /**
     * 添加文件
     *
     * @param files
     * @return
     */
    @RequestMapping(value = "insertFile", method = RequestMethod.POST)
    public String goodsListAdd(File files, String date) {

        files.setCreateTimes(date);
        if (files.getFile_num() == null) {
            files.setFile_num(0);
        }
        HtmlUtil.htmlEscape(files);
        fileService.insert(files);

        return "redirect:fileListShow";
    }


    /**
     * 跳转中转站
     *
     * @param
     * @return
     */
    @RequestMapping(value = "addPicture")
    public String addPicture(@RequestParam("file_id") Integer file_id, Map<String, Object> map) {
        Map<String, Object> params = new HashMap<String, Object>();
        File files = fileService.getById("getById", file_id);
        map.put("files", files);
        return "home/admin/file/add_picture";
    }


    /**
     * 图片储存
     * value = "file" 对应接受的字段值,MultipartFile file中: fil（变量）
     *
     * @param multipartFiles
     * @return (value = "file", required = false) MultipartFile[] file
     */
    @RequestMapping(value = "addFile", method = {RequestMethod.GET, RequestMethod.POST})
    public String imageUploa(FilePicture filePicture,
                             @RequestParam("file") MultipartFile[] multipartFiles,
                             HttpServletRequest request) throws IllegalStateException, IOException {

        ImageUtil imageUtil = new ImageUtil();
        if (multipartFiles != null && multipartFiles.length > 0) {
            //循环获取file数组中得文件
            for (MultipartFile multipartFile : multipartFiles) {
                //得到图片地址
                String amageurl = imageUtil.ImageUpload(multipartFile, request.getSession());
                //替换字符串中所有指定的字符
                System.out.println("---------------------------------" + amageurl);
                filePicture.setPictures(amageurl);
                filePictureService.insert(filePicture);
            }
        }
        return "redirect:fileListShow";
    }


    /**
     * 文件列表
     *
     * @return
     */
    @RequestMapping("fileListShow")
    public String fileListShow(File file, HttpServletRequest request, Map<String, Object> map) {
        Map<String, Object> params = new HashMap<>();

            // 判断长度，是否为空
            if (StringUtils.isNotBlank(file.getFile_name())) {
                params.put("file_name", "%" + file.getFile_name() + "%");
            }
            if (file.getCreateTimes() != null && !file.getCreateTimes().equals("")) {
                params.put("createTimes", file.getCreateTimes());
            }

        PageUtil<File> page = fileService.page(request, 10, params);
        if(page.getLists().size()==0){
            map.put("noFile", "没有找到你想要的数据！");
        }
        map.put("page", page);
        return "home/admin/file/file_show";
    }


    /**
     * 回显信息--用于修改前回显
     *
     * @return
     */
    @RequestMapping("getByIdFile")
    public String getByIdFile(Map<String, Object> map, @RequestParam("file_id") Integer file_id) {
        File files = fileService.getById("getById", file_id);
        map.put("files", files);
        return "home/admin/file/file_update";
    }

    /**
     * File表修改
     *
     * @return
     */
    @RequestMapping("updateFile")
    public String updateFile(File file) {
        fileService.update("update", file);
        return "redirect:fileListShow";
    }


    /**
     * 删除全部信息（包括图片）
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("deleteFileAndPictures")
    public String deleteFileAndPictures(@RequestParam("file_id") Integer file_id) {
        File file = new File();
        file.setFile_id(file_id);
        fileService.delete("delete", file.getFile_id());
        filePictureService.delete("delete", file_id);
        return "redirect:fileListShow";
    }

    /**
     * 图片展示
     *
     * @return
     */
    @RequestMapping("pictureShow")
    public String pictureShow(Map<String, Object> map, @RequestParam("file_id") Integer file_id) {
        Map<String, Object> params = new HashMap<>();
        params.put("file_id", file_id);
        List<FilePicture> filePictures = filePictureService.listAll("listAll", params);
        if(filePictures.size()==0){
            map.put("errors","没有图片，请先上传图片！");
        }
        map.put("filePictures", filePictures);
        return "home/admin/file/picture_show";
    }


    /**
     * 删除图片
     * @param
     * @param
     * @return
     */
    @RequestMapping("deletePictures")
    public String deletePictures(Map<String, Object> map,
                                 @RequestParam("pictures") String pictures) {
        Map<String, Object> params = new HashMap<>();
        params.put("pictures",pictures);
        List<FilePicture> filePicture= filePictureService.listAll("listAlls",params);
        Integer id  = filePicture.get(0).getId();
        filePictureService.deletes("deletes",id);
        return "redirect:fileListShow";
    }

}
